package com.gljx.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.web.Entity.OwnCarTrain;
import com.gljx.web.Entity.OwnCarTrainEntry;
import com.gljx.web.Entity.User;
import com.gljx.web.Entity.JournalEntry.ExaminationRoomIncome;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.OwnCarTrainEntryMapper;
import com.gljx.web.dao.OwnCarTrainMapper;
import com.gljx.web.service.OwnCarTrainService;

@Service
@Transactional
public class OwnCarTrainServiceImpl extends CommonDao implements OwnCarTrainService{

	@Autowired
	private OwnCarTrainMapper ownCarTrainMapper;
	
	@Autowired
	private OwnCarTrainEntryMapper ownCarTrainEntryMapper;
	
	
	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		  Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Pindex", pindex);
	       paramMap.put("Psql", psql);
	       paramMap.put("Psize", psize);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.OwnCarTrainMapper.pagemap", paramMap, null);
	       return  paramMap;
	}

	/**
	 * 取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Psql", psql);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       this.getSqlSession().select("com.gljx.web.dao.OwnCarTrainMapper.pagecount", paramMap, null);
	       return paramMap;
	}

	/**
	 * 根据条件查询子类信息
	 */
	@Override
	public List<OwnCarTrainEntry> selectOwnCarTrainEntry(String fparentId, String cfchepai, String divingName) {
		// TODO Auto-generated method stub
		   Map<String, Object> paramMap = new HashMap<String, Object>();
		   paramMap.put("v_fparentid", fparentId);
	       paramMap.put("v_cfchepai", cfchepai);
	       paramMap.put("v_drivingName", divingName);
	       paramMap.put("v_cursor", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.OwnCarTrainEntryMapper.selectOwnCarTrainEntry", paramMap, null);
	       @SuppressWarnings("unchecked")
		   List<OwnCarTrainEntry>ownCarTrainEntrys = (List<OwnCarTrainEntry>) paramMap.get("v_cursor");
		   return ownCarTrainEntrys;
	}

	/**
	 * 添加或修改
	 */
	@Override
	public void editOwnCarTrain(OwnCarTrain ownCarTrain, String[] fseq, String[] cfchepai, String[] cfchexing,String[] cfkemu,String[] cfcartype,
			String[] cfxyjsid, String[] cfprice, String[] cfamount, String[] cfqty, String[] cfdesc,String []cfcarnumber, User user,
			String time) {
		// TODO Auto-generated method stub
		if("".equals(ownCarTrain.getFid()) || ownCarTrain.getFid() == null){
			//创建人id
			ownCarTrain.setFcreatorid(user.getFid());
			//利用UUID生成fid
			ownCarTrain.setFid(GetFid.UUIDIntoFid());
			//当前时间
			ownCarTrain.setFcreatetime(time);
			//状态
            ownCarTrain.setCfbillstate(3L);
            //添加
			ownCarTrainMapper.insertSelective(ownCarTrain);
		}else{
			//修改时间时间
			ownCarTrain.setFlastupdatetime(time);
			//修改人id
			ownCarTrain.setFlastupdateuserid(user.getFid());
			//修改
			ownCarTrainMapper.updateByPrimaryKeySelective(ownCarTrain);
			//先删除子类信息
			ownCarTrainEntryMapper.deleteByFparentId(ownCarTrain.getFid());
		}
		//添加子类
		List<OwnCarTrainEntry> ownCarTrainEntrys = new ArrayList<>();
		if(fseq == null || fseq.length == 0){
			
		}else{
			for (int i = 0; i < fseq.length; i++) {
				OwnCarTrainEntry ownCarTrainEntry = new OwnCarTrainEntry();
				ownCarTrainEntry.setCfchepai(cfchepai[i]);
				if("".equals(cfchexing[i])){
					
				}else{
					ownCarTrainEntry.setCfchexing(BigDecimalFormat.formot(cfchexing[i]));
				}
				ownCarTrainEntry.setCfkemu(Long.parseLong(cfkemu[i]));
				ownCarTrainEntry.setCfcartype(cfcartype[i]);
				ownCarTrainEntry.setCfxyjsid(cfxyjsid[i]);
				if("".equals(cfprice[i])){
					
				}else{
					ownCarTrainEntry.setCfprice(BigDecimalFormat.formot(cfprice[i]));
				}
				if("".equals(cfamount[i])){
					
				}else{
					ownCarTrainEntry.setCfamount(BigDecimalFormat.formot(cfamount[i]));
				}
				if("".equals(cfqty[i])){
					
				}else{
					ownCarTrainEntry.setCfqty(Long.parseLong(cfqty[i]));
				}
				//rhc
				if("".equals(cfcarnumber[i])){

				}else{
					ownCarTrainEntry.setCfcarnumber(cfcarnumber[i]);
				}
				ownCarTrainEntry.setCfdesc(cfdesc[i]);
				ownCarTrainEntry.setFseq((long) i+1);
				ownCarTrainEntry.setFid(GetFid.UUIDIntoFid());
				ownCarTrainEntry.setFparentid(ownCarTrain.getFid());
				ownCarTrainEntrys.add(ownCarTrainEntry);
			}
			ownCarTrainEntryMapper.insertOwnCarTrainEntry(ownCarTrainEntrys);
		}
	}

	/**
	 * 根据fid查询
	 */
	@Override
	public OwnCarTrain queryOwnCarTrainByFid(String fid) {
		// TODO Auto-generated method stub
		return ownCarTrainMapper.selectOwnCarTrainByFid(fid);
	}

	/**
	 * 根据fid删除
	 */
	@Override
    public void deleteOwnCarTrainByFid(String[] deleteId, String fid, String[] fnumber, String fnumber2) {
        // TODO Auto-generated method stub
        ActitiviUtil actitiviUtil = new ActitiviUtil();
        if(fid == null || "".equals(fid)){
            for (int i = 0; i < deleteId.length; i++) {
                //先删除子类信息
                ownCarTrainEntryMapper.deleteByFparentId(deleteId[i]);
                //在删除父类信息
                ownCarTrainMapper.deleteByPrimaryKey(deleteId[i]);
                //删除流程
                actitiviUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber[i]);
            }
        }else{
			//先删除子类信息
			ownCarTrainEntryMapper.deleteByFparentId(fid);
			//在删除父类信息
			ownCarTrainMapper.deleteByPrimaryKey(fid);
            //删除流程
            actitiviUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber2);
        }
	}

	/**
	 * 审核，反审核
	 */
	@Override
	public int auditOwnCarTrain(String fid, String type, String fhandlerId, String message, String state, String fnumber) {
		// TODO Auto-generated method stub
		OwnCarTrain ownCarTrain = new OwnCarTrain();
        Axamine axamine = new Axamine();
        //fid
		ownCarTrain.setFid(fid);
        if("1".equals(type)){
			ServerResponse serverResponse = axamine.auditProcess(fnumber, state, message);
			if (serverResponse.getStatus() == 1) {
				ownCarTrain.setCfbillstate(5L);
                //审核人
                ownCarTrain.setFauditorid(fhandlerId);
                //修改人
                ownCarTrain.setFlastupdateuserid(fhandlerId);
                //结束
				return ownCarTrainMapper.updateByPrimaryKeySelective(ownCarTrain);
			} else if (serverResponse.getStatus() == 2) {
				//推进
                if (!"1".equals(state)) {
                    ownCarTrain.setCfbillstate(3L);
                    ownCarTrainMapper.updateByPrimaryKeySelective(ownCarTrain);
                }
                return 2;
			} else {
				//错误
				return 0;
			}

		}else{
            axamine.againstAuditProcess(fnumber, message);
            ownCarTrain.setCfbillstate(3L);
			return ownCarTrainMapper.updateByPrimaryKeySelective(ownCarTrain);
		}
	}

	/**
	 * 根据条件查询考场收入统计报表
	 */
	@Override
	public List<ExaminationRoomIncome> queryExaminationRoomIncome(String startTime, String endTime, String companyId,
			String type,String drivingName) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("companyId", companyId);
		map.put("type", type);
		map.put("drivingName", drivingName);
		return ownCarTrainEntryMapper.selectExaminationRoomIncome(map);
	}

    @Override
    public int modifyOwnCarTrain(OwnCarTrain ownCarTrain) {
        return ownCarTrainMapper.updateByPrimaryKeySelective(ownCarTrain);
    }

    @Override
    public List<OwnCarTrainEntry> selectOwnCarTrainByInfo(String fcompanyid, String cfchepai, String drivingName, String chergetype, String beginTime, String endTime) {
        Map<String, String> map = new HashMap<>();
        map.put("cfchepai", cfchepai);
        map.put("drivingName", drivingName);
        map.put("chergetype", chergetype);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("fcompanyid", fcompanyid);
        return ownCarTrainEntryMapper.selectOwnCarTrainByInfo(map);
    }

}
